package com.pl.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.pl.bean.ManageUserBean;
import com.pl.common.Constant.sessionKey;
import com.pl.service.ManagePageService;
import com.pl.service.ManageUserService;

@Service
public class UserDetailServiceImpl implements UserDetailsService{
	
	private static Logger log = LogManager.getLogger();
	
	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
		
	@Resource(name="managePageService")
	private ManagePageService managePageService;
	
	@Transactional
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = servletRequestAttributes.getRequest();
		String imageCode = request.getParameter("imageCode");
		
		log.debug("imageCode = {}", imageCode);
		
		if(Strings.isNullOrEmpty(imageCode)) {
			throw new UsernameNotFoundException("图形验证码不可为空");
		}
		
		String sessionImageCode = (String) request.getSession().getAttribute(sessionKey.LOGIN_IMAGE_CODE.getValue());
		if(!Objects.equal(imageCode, sessionImageCode)) {
			throw new UsernameNotFoundException("图形验证码错误");
		}
		
		if(Strings.isNullOrEmpty(username)) {
			throw new UsernameNotFoundException("空的用户名");
		}
		
		ManageUserBean manageUserBean = manageUserService.getManageUserByUserName(username);
		
		if(manageUserBean == null) {
			throw new UsernameNotFoundException("用户不存");
		}
		
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        
        //获取用户的授权
        //List<ManagePageBean> pageList = managePageService.getManagePageByUser(manageUserBean.getId());
        
        //声明授权文件
        /*for (ManagePageBean page : pageList) {
        	GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(page.getId()+"");
            grantedAuthorities.add(grantedAuthority);
        }*/
        
        GrantedAuthority g1 = new SimpleGrantedAuthority("ROLE_ADMIN");
        grantedAuthorities.add(g1);
        
        User user = new User(manageUserBean.getUser_name(), manageUserBean.getPassword(), grantedAuthorities);
        System.out.println(user);
        
        return user;
    }
}	
